package com.myorg.service;

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;

//TODO: Auto-wiring is not there yet.
public class SparkService {
	@SuppressWarnings("serial")
	public static void main(String[] args) {
		JavaSparkContext jsc = new JavaSparkContext("spark://hndev:7077", "Spark Test",
				System.getenv("SPARK_HOME"), JavaSparkContext.jarOfClass(SparkService.class));

		jsc.addJar("./Search.jar");
		
		String logFile = "hdfs://localhost:9000/in/c.txt";
		JavaRDD<String> logData = jsc.textFile(logFile).cache();

		long numAs = logData.filter(new Function<String, Boolean>() {
			public Boolean call(String s) {
				return s.contains("A");
			}
		}).count();

		long numBs = logData.filter(new Function<String, Boolean>() {
			public Boolean call(String s) {
				return s.contains("B");
			}
		}).count();

		System.out.println("Lines with a: " + numAs + ", lines with b: " + numBs);

		jsc.close();
	}
}
